package physics;

import entities.Bullet;
import entities.GameCharacter;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Polygon;

/**
 * Physics
 * @author Fede
 * @data 27-abr-2011
 * @versió v1.0
 * Copyright Federico Ferran <tresefes@hotmail.com>
 * Descripció:
 *
 * 
 */
public class Physics {

    private float acG = 0.1f;

    public boolean gentityCollisionWith(Polygon p) throws SlickException {
//        System.out.println(BlockMap.entities.size());
        for (int i = 0; i < BlockMap.entities.size(); i++) {
            Block entity1 = (Block) BlockMap.entities.get(i);
            if (p.intersects(entity1.poly)) {
                return true;
            }
        }
        return false;
    }

    public Polygon entityCollisionWith(Polygon p) throws SlickException {
//        System.out.println(BlockMap.entities.size());
        for (int i = 0; i < BlockMap.entities.size(); i++) {
            Block entity1 = (Block) BlockMap.entities.get(i);
            if (p.intersects(entity1.poly)) {
                return entity1.poly;
            }
        }
        return null;
    }

    public void gravity(int delta, GameCharacter e) {
        e.setSaltable(false);
        Polygon poly2 = e.getPoly().copy();
        try {
            if (e.getVelY() >= 0) {
                poly2.setY(e.getPosY() + e.getMargenUp() + 1);
                if (!gentityCollisionWith(poly2)) {
                    e.setVelY(e.getVelY() + acG * (delta / 10));
                } else {
                    e.setSaltable(true);
                    e.setVelY(0.0f);
                }
                if (e.getVelY() > 5) {
                    e.setVelY(5);
                }
            } else if (e.getVelY() < 0) {
                poly2.setY(e.getPosY() + e.getMargenUp() - 1);
                if (!gentityCollisionWith(poly2)) {
                    e.setVelY(e.getVelY() + acG * (delta / 10));
                } else {
                    e.setVelY(0.0f);
                }
                if (e.getVelY() < -5) {
                    e.setVelY(-5);
                }
            }

        } catch (SlickException ex) {
            System.out.println(ex);
        }
    }

//    public float distY(float vel, int delta, Polygon poly, float posY) {
//        Polygon poly2 = poly.copy();
//        try {
//
//            float dist = (float) (vel * (delta / 10) + 0.5 * acG * (delta / 10) * (delta / 10));
//            if (dist >= 1) {
//                for (float i = 0; i <= dist; i++) {
//                    poly2.setY(posY + 27 + i + 1);
//                    if (entityCollisionWith(poly2)) {
//                        return (int) i;
//                    }
//                }
//                return (int) dist;
//            } else if (dist <= -1) {
//                for (float i = 0; i >= dist; i--) {
//                    poly2.setY(posY + 27 - i - 3);
//                    if (entityCollisionWith(poly2)) {
//                        return (int) i;
//
//                    }
//                }
//                return (int) dist;
//            }
//
//        } catch (SlickException ex) {
//            System.out.println(ex);
//            return 0;
//        }
//        return 0;
//    }
    public void distY(int delta, GameCharacter e) {
        Polygon poly2 = e.getPoly().copy();
        try {
            float dist = (float) (e.getVelY() * (delta / 10) + 0.5 * acG * (delta / 10) * (delta / 10));
            if (dist > 0) {
                poly2.setY(e.getPosY() + e.getMargenUp() + dist);
                Polygon poly = entityCollisionWith(poly2);
                if (poly != null) {
                    e.setPosY(poly.getY() - 71.2f);
                } else {
                    e.setPosY(e.getPosY() + dist);
                }
            } else if (dist < 0) {
                poly2.setY(e.getPosY() + e.getMargenUp() + dist);
                Polygon poly = entityCollisionWith(poly2);
                if (poly != null) {
                    e.setPosY((poly.getY() - e.getMargenUp())+16.1f);
                } else {
                    e.setPosY(e.getPosY() + dist);
                }
            }

        } catch (SlickException ex) {
            System.out.println(ex);
        }
    }

//    public float distX(float vel, int delta, Polygon poly, float posX) {
//        Polygon poly2 = poly.copy();
//        try {
//            float dist = vel * (delta / 10);
//            if (dist >= 1) {
//                for (float i = 0f; i <= dist; i++) {
//                    poly2.setX(posX + 25 + i + 2);
//                    if (gentityCollisionWith(poly2)) {
//                        return (int) i;
//                    }
//                }
//                return dist;
//            } else if (dist <= -1) {
//                for (float i = 0f; i >= dist; i--) {
//                    poly2.setX(posX + 25 + i - 2);
//                    if (gentityCollisionWith(poly2)) {
//                        return (int) i;
//                    }
//                }
//                return dist;
//            }
//            return dist;
//
//        } catch (SlickException ex) {
//            System.out.println(ex);
//            return 0;
//        }
//    }

    public void distX(int delta, GameCharacter e) {
        Polygon poly2 = e.getPoly().copy();
        try {
            float dist = e.getVelX() * (delta / 10);
            if (dist < 0) {
                poly2.setX(e.getPosX() + e.getMargenDown() + dist);
                Polygon poly = entityCollisionWith(poly2);
                if (poly != null) {
                    e.setPosX(poly.getX() - e.getMargenDown()+15.1f);
                } else {
                    e.setPosX(e.getPosX() + dist);
                }
            } else if (dist > 0) {
                poly2.setX(e.getPosX()+ e.getMargenDown() + dist);
                Polygon poly = entityCollisionWith(poly2);
                if (poly != null) {
                    e.setPosX(poly.getX()-e.getMargenDown()-16.1f);
                } else {
                    e.setPosX(e.getPosX() + dist);
                }
            }

        } catch (SlickException ex) {
            System.out.println(ex);
        }
    }

//    public float bulletdist(float vel, int delta, Polygon poly, float posX, Bullet b) {
//        float dist = vel * (delta / 10);
//        return dist;
//    }
    public float bulletdist(float vel, int delta, Polygon poly, float posX, Bullet b) {
        Polygon poly2 = poly.copy();
        try {
            float dist = vel * (delta / 10);
            if (dist > 0) {
                for (float i = 0f; i <= dist; i++) {
                    poly2.setX(posX + i + 1);
                    if (gentityCollisionWith(poly2)) {
                        b.setLost(true);
                    }
                }
                return dist;
            } else if (dist < 0) {
                for (float i = 0f; i >= dist; i--) {
                    poly2.setX(posX - i - 1);
                    if (gentityCollisionWith(poly2)) {
                        b.setLost(true);
                    }
                }
                return dist;
            }
            return dist;

        } catch (SlickException ex) {
            System.out.println(ex);
            return 0;
        }
    }

    public boolean entityToEntityCollision(Polygon enPoly, Polygon poly) throws SlickException {
        if (enPoly.intersects(poly)) {
            return true;
        }
        return false;
    }
}
